package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAppBookchapters;
import com.ruoyi.system.domain.SysAppBookinfo;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.service.ISysAppBookchaptersService;
import com.ruoyi.system.service.ISysAppBookinfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 书籍章节Controller
 * 
 * @author ruoyi
 * @date 2020-09-07
 */
@Controller
@RequestMapping("/system/bookchapters")
public class SysAppBookchaptersController extends BaseController
{
    private String prefix = "system/bookchapters";

    @Autowired
    private ISysAppBookchaptersService sysAppBookchaptersService;

    @RequiresPermissions("system:bookchapters:view")
    @GetMapping()
    public String bookchapters()
    {
        return prefix + "/bookchapters";
    }

    /**
     * 查询书籍章节列表
     */
    @RequiresPermissions("system:bookchapters:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysAppBookchapters> list(SysAppBookchapters sysAppBookchapters)
    {
        startPage();
        List<SysAppBookchapters> list = sysAppBookchaptersService.selectSysAppBookchaptersList(sysAppBookchapters);
        for(SysAppBookchapters chapters:list) {
        	sysAppBookchapters.setParentId(chapters.getDeptId());
        	int i = sysAppBookchaptersService.selectSysAppBookchaptersCount(sysAppBookchapters);
        	chapters.setIsNode(i);
        }
        return list;
    }

    /**
     * 导出书籍章节列表
     */
    @RequiresPermissions("system:bookchapters:export")
    @Log(title = "书籍章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAppBookchapters sysAppBookchapters)
    {
        List<SysAppBookchapters> list = sysAppBookchaptersService.selectSysAppBookchaptersList(sysAppBookchapters);
        ExcelUtil<SysAppBookchapters> util = new ExcelUtil<SysAppBookchapters>(SysAppBookchapters.class);
        return util.exportExcel(list, "bookchapters");
    }
    
    /**
     * 新增书籍章节
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
    	SysAppBookchapters chapters = sysAppBookchaptersService.selectSysAppBookchaptersById(parentId);
    	if(null == chapters) {
    		chapters = new SysAppBookchapters();
    	}
        mmap.put("parentId", parentId);
        mmap.put("chapters", chapters);
        return prefix + "/add";
    }

    /**
     * 新增保存书籍章节
     */
    @RequiresPermissions("system:bookchapters:add")
    @Log(title = "书籍章节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysAppBookchapters sysAppBookchapters)
    {
        return toAjax(sysAppBookchaptersService.insertSysAppBookchapters(sysAppBookchapters));
    }

    /**
     * 修改书籍章节
     */
    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {
        SysAppBookchapters sysAppBookchapters = sysAppBookchaptersService.selectSysAppBookchaptersById(deptId);
        mmap.put("sysAppBookchapters", sysAppBookchapters);
        return prefix + "/edit";
    }

    /**
     * 修改保存书籍章节
     */
    @RequiresPermissions("system:bookchapters:edit")
    @Log(title = "书籍章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysAppBookchapters sysAppBookchapters)
    {
        return toAjax(sysAppBookchaptersService.updateSysAppBookchapters(sysAppBookchapters));
    }

    /**
     * 删除书籍章节
     */
    @RequiresPermissions("system:bookchapters:remove")
    @Log(title = "书籍章节", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{deptId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("deptId") Long deptId)
    {
    	SysAppBookchapters sysAppBookchapters = new SysAppBookchapters();
    	sysAppBookchapters.setParentId(deptId);
    	int i = sysAppBookchaptersService.selectSysAppBookchaptersCount(sysAppBookchapters);
    	 if (i > 0)
         {
             return AjaxResult.warn("存在下级章节,不允许删除");
         }
    	
        return toAjax(sysAppBookchaptersService.deleteSysAppBookchaptersByIds(deptId+""));
    }
    
    /**
     * 查询字典详细
     */
    @RequiresPermissions("system:bookchapters:add")
    @GetMapping("/detail/{deptId}")
    public String detail(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {
    	SysAppBookchapters chapters = sysAppBookchaptersService.selectContentById(deptId);
        mmap.put("deptId", deptId);
        mmap.put("sysAppBookchapters", chapters);
        return prefix + "/addContent";
    }
}
